/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:CommodityAction.java
 * Version:1.0
 * Date:2011-12-17
 * Description: 
 * author:TT_LOVE
 */
package com.tietiao.action.commodity;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.tietiao.common.action.BaseAction;
import com.tietiao.common.util.StringUtil;
import com.tietiao.dao.model.CommodityInfo;
import com.tietiao.dao.model.Lable;
import com.tietiao.dao.model.SysUser;
import com.tietiao.dao.model.UserComment;
import com.tietiao.service.commodity.CommodityService;

@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class CommodityAction extends BaseAction {
	@Resource
	private CommodityService commodityService;
	// 商品列表信息
	private List<CommodityInfo> commodityInfoList;
	// 标签信息
	private List<String> lableList = new ArrayList<String>();;
	// 用户评论信息
	private List<UserComment> userCommentList = new ArrayList<UserComment>();
	// 用户信息表
	private List<SysUser> userList = new ArrayList<SysUser>();
	// 排序
	private String orderType = "TIETIAO_NUM DESC";
	
	// 商品信息
	private CommodityInfo commodityInfo;
	// 用户信息
	private SysUser userInfo = new SysUser();
	
	private List<Lable> lableInfoList = new ArrayList<Lable>();

	/**
	 * 未登陆主页面 功能：
	 * 
	 * @author: TT_LOVE
	 * @Date: 2012-1-3
	 * 
	 * @return
	 */
	public String index() {
		
		// 获取登录用户信息
		userInfo = getUser();
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：加载商品信息
	 * 
	 * @author: TT_LOVE
	 * @Date: 2012-1-3
	 * 
	 * @return
	 * @throws Exception
	 */
	public String loadCommodityInfo() throws Exception {
		Integer userId = null;
		if (getUser() != null) {
			userId = getUser().getUserId();
			int order = Integer.parseInt(request.getParameter("order"));
			orderType = setOrderType(order);
		}
		String lableName = request.getParameter("lableName");
		if(StringUtils.isNotBlank(lableName)){
			lableName = StringUtil.decode(lableName);
		}
		String lables = request.getParameter("lables");
		if (StringUtils.isNotBlank(lables)) {
			lables = StringUtil.decode(lables);
			commodityInfoList = commodityService.searchCommodityInfoByLable(lables, userId, null, null, orderType);
		} else {
			if(getUser() == null || StringUtils.isNotBlank(lableName)){
				commodityInfoList = commodityService.searchCommodityInfos(lableName,null, null, userId, null, null, orderType);
			}else{
				commodityInfoList = commodityService.searchCommodityInfoByUserId(userId,orderType);
			}
		}
		return DEFAULT;
	}

	/**
	 * 功能：首页排序
	 * @author: TT_LOVE
	 * @Date: 2012-2-11
	 *
	 * @param order
	 * @return
	 */
	private String setOrderType(int order) {
		switch (order) {
			case 0:
				orderType = "C.UPLOADER_TIME DESC"; // 时间降序
				break;
			case 1:
				orderType = "C.UPLOADER_TIME ASC";  // 时间升序
				break;
			case 2:
				orderType = "C.TIETIAO_NUM ASC";    // 贴条数升序			
				break;
			case 3:
				orderType = "C.TIETIAO_NUM DESC";   // 贴条数降序
				break;
		}
		return orderType;
	}

	/**
	 * 
	 * 功能：商品详细信息
	 * 
	 * @author: TT_LOVE
	 * @Date: 2012-1-3
	 * 
	 * @return
	 */
	public String commodityDetail() {
		Integer userId = null;
		if (getUser() != null) {
			userId = getUser().getUserId();
		}
		int commodityId = Integer.parseInt(request.getParameter("commodityId"));
		commodityInfo = commodityService.getCommodityInfoByCommodityId(commodityId, userId);
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：加载标签与评论信息
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2012-1-5
	 * 
	 * @return
	 */
	public String loadLableInfo() {
		int commodityId = Integer.parseInt(request.getParameter("commodityId"));
		commodityInfo = commodityService
				.getCommodityInfoByCommodityId(commodityId);

		// 获取标签信息
		if (StringUtils.isNotBlank(commodityInfo.getLables())) {
			String[] lableArr = commodityInfo.getLables().split(" ");
			for (String lableName : lableArr) {
				lableList.add(lableName);
			}
		}

		// 获取登录用户信息
		userInfo = getUser();

		// 获取评论信息
		orderType = "COMMENT_TIME";
		userCommentList = commodityService.getCommentInfoByCommodityId(commodityId, orderType,-1);
		for(UserComment userComment : userCommentList){
			List<UserComment> commentList = commodityService.getCommentInfoByCommodityId(commodityId, orderType,userComment.getCommentId());
			userComment.setCommentList(commentList);
		}
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：推荐用户信息
	 * 
	 * @author: TT_LOVE
	 * @Date: 2012-1-10
	 * 
	 * @return
	 */
	public String commend() {
//		System.out.println(userInfo.getPetName());
//		userList = commodityService.getCommendUser(getUser().getUserId(),userInfo.getPetName());
		return DEFAULT;
	}
	
	public String friendList(){
		String petName = request.getParameter("petName");
		userList = commodityService.getCommendUser(getUser().getUserId(),petName);
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：推荐用户
	 * 
	 * @author: TT_LOVE
	 * @Date: 2012-1-10
	 * 
	 */
	public void commendUser() {
		webRoot= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+webRoot;
		int commodityId = Integer.parseInt(request.getParameter("commodityId"));
		String[] userIdArr = request.getParameterValues("userCheckbox");
		commodityService.sendCommodityInfoToCommendUser(commodityId, userIdArr,
				getUser(), webRoot);
	}

	/**
	 * 
	 * 功能：用户评论
	 * 
	 * @author: TT_LOVE
	 * @Date: 2012-1-10
	 * 
	 */
	public void submitComment() {
		int commodityId = Integer.parseInt(request.getParameter("commodityId"));
		String commentInfo = request.getParameter("commentInfo");
		commodityService.submitComment(getUser(), commodityId, commentInfo);
	}

	public void submitReplyInfo(){
		int commodityId = Integer.parseInt(request.getParameter("commodityId"));
		String commentId = request.getParameter("commentId");
		String contentInfo = request.getParameter("contentInfo");
		commodityService.submitContentInfo(getUser(), commodityId, commentId, contentInfo);
	}
	
	/**
	 * 
	 * 功能：查询标签
	 * @author: TT_LOVE
	 * @Date: 2012-2-19
	 *
	 * @return
	 */
	public String lableInfo(){
		lableInfoList = commodityService.getLableInfoList();
		return DEFAULT;
	}
	
	/**
	 * @return the commodityService
	 */
	public CommodityService getCommodityService() {
		return commodityService;
	}

	/**
	 * @param commodityService
	 *            the commodityService to set
	 */
	public void setCommodityService(CommodityService commodityService) {
		this.commodityService = commodityService;
	}

	/**
	 * @return the commodityInfoList
	 */
	public List<CommodityInfo> getCommodityInfoList() {
		return commodityInfoList;
	}

	/**
	 * @param commodityInfoList
	 *            the commodityInfoList to set
	 */
	public void setCommodityInfoList(List<CommodityInfo> commodityInfoList) {
		this.commodityInfoList = commodityInfoList;
	}

	/**
	 * @return the orderType
	 */
	public String getOrderType() {
		return orderType;
	}

	/**
	 * @param orderType
	 *            the orderType to set
	 */
	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	/**
	 * @return the rootPicPath
	 */
	public String getRootPicPath() {
		return rootPicPath;
	}

	/**
	 * @param rootPicPath
	 *            the rootPicPath to set
	 */
	public void setRootPicPath(String rootPicPath) {
		this.rootPicPath = rootPicPath;
	}

	/**
	 * @return the commodityInfo
	 */
	public CommodityInfo getCommodityInfo() {
		return commodityInfo;
	}

	/**
	 * @param commodityInfo
	 *            the commodityInfo to set
	 */
	public void setCommodityInfo(CommodityInfo commodityInfo) {
		this.commodityInfo = commodityInfo;
	}

	/**
	 * @return the lableList
	 */
	public List<String> getLableList() {
		return lableList;
	}

	/**
	 * @param lableList
	 *            the lableList to set
	 */
	public void setLableList(List<String> lableList) {
		this.lableList = lableList;
	}

	/**
	 * @return the userInfo
	 */
	public SysUser getUserInfo() {
		return userInfo;
	}

	/**
	 * @param userInfo
	 *            the userInfo to set
	 */
	public void setUserInfo(SysUser userInfo) {
		this.userInfo = userInfo;
	}

	/**
	 * @return the userCommentList
	 */
	public List<UserComment> getUserCommentList() {
		return userCommentList;
	}

	/**
	 * @param userCommentList
	 *            the userCommentList to set
	 */
	public void setUserCommentList(List<UserComment> userCommentList) {
		this.userCommentList = userCommentList;
	}

	/**
	 * @return the userList
	 */
	public List<SysUser> getUserList() {
		return userList;
	}

	/**
	 * @param userList
	 *            the userList to set
	 */
	public void setUserList(List<SysUser> userList) {
		this.userList = userList;
	}

	/**
	 * @return the lableInfoList
	 */
	public List<Lable> getLableInfoList() {
		return lableInfoList;
	}

	/**
	 * @param lableInfoList the lableInfoList to set
	 */
	public void setLableInfoList(List<Lable> lableInfoList) {
		this.lableInfoList = lableInfoList;
	}
}
